app.controller('newRoleCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	var token = $window.localStorage.getItem("token");
    $scope.init = function(){
    	
    	$scope.iNowData = {
    		"id": "",
    		"roleName": "",
    		"remark": "",
    		"objectLookup.id": ""
    	};
    	
    	//初始化获取所有角色
		rest.decoJava({
	       	type: 'POST',
	        url: '/tenancy-sys/admin/role/all/list',
	        data: "",
	       	beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
	        success: function(data){
	            $scope.upRoleDatas = data.result;
	            $scope.upRoleDatas.unshift({roleName: "请选择", id: ""});
	            $scope.upRole = $scope.upRoleDatas[0];
	            $scope.$apply();
	        }
	    })
    };
    $scope.init();
    
	//保存
    $scope.preserveSubmit = function(){
    	if($scope.iNowData.id == ""){  //新建角色
	    	//新建按钮进来判断是否选择了所属角色
	    	if($scope.upRole == null){
	    		var objectLookupId = "";  //没有选所属角色则初始化为空 即角色等级为0
	    	} else{
	    		var objectLookupId = $scope.upRole.id;  //菜单等级为1
	    	};
	    	var formData = {
	    		"roleName": $scope.iNowData.roleName,
	    		"remark": $scope.iNowData.remark,
	    		"upId": objectLookupId
	    	};
	    	var btn = true;
	    	if($scope.iNowData.roleName == ""){
	    		layer.tips('角色名称必填', '.roleName-Tips',{
				  tipsMore: true,
				  tips: [2,'#ed5565']
				});
				btn = false;
	    	};
	    	if($scope.iNowData.remark == ""){
	    		layer.tips('备注说明必填', '.remark-Tips',{
				  tipsMore: true,
				  tips: [2,'#ed5565']
				});
				btn = false;
	    	};
	    	if(btn){
		    	rest.decoJava({
		    		type: 'post',
		            url: '/tenancy-sys/admin/role/add',
		            data: JSON.stringify(formData),
		            contentType: "application/json; charset=utf-8",
		            beforeSend: function(request){
						request.setRequestHeader("token", token); 
					},
		            success: function (result) {
		            	layer.closeAll('loading');
		                if (result) {
		                    if ("0000" == result.code) {
								layer.msg(result.message, {
						        	offset:'t',
						        	icon: 1,
									time: 0, //不自动关闭
									btn: ['确定'],
									yes: function(index){
										layer.close(index);
										$scope.closeThisDialog();
								    }
								});
		                    } else {
		                    	layer.msg(result.message,{
				    				offset:'t'
				    			});
		                    }
		                }
		            }
		        });
	    	};
	    	
	    }
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
